package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.bean.UserGiftRecordBean;
import com.taptech.doufu.ui.view.UserGradeView;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.TMAnalysis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by longsiyang on 16/1/8.
 */
public class GiftUserListAdapter extends RecyclerView.Adapter<GiftUserListAdapter.GiftUserItemViewHolder> {

    Context mContext;
    List<UserGiftRecordBean> mDataList ;

    public GiftUserListAdapter(Context context , List<UserGiftRecordBean> dataList){
        mContext = context;
        mDataList = dataList;

    }


    @Override
    public GiftUserItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.user_gift_record_item_layout , parent , false);
        GiftUserItemViewHolder viewHolder = new GiftUserItemViewHolder(mContext , view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(GiftUserItemViewHolder holder, int position) {
        holder.setViewHolderChildViewContent(mDataList.get(position) , position);
    }

    @Override
    public int getItemCount() {
        if (mDataList != null){
            return mDataList.size();
        }
        return 0;
    }

    class GiftUserItemViewHolder extends BaseRecyclerViewViewHolder implements View.OnClickListener{
        RelativeLayout itemLayout;
        TextView rankPos;
        ImageView userIcon;
        TextView userGiftRecord , userGiftCoins;
        ImageView rankImg;
        UserGiftRecordBean bean;
        SimpleDraweeView bestGiftIcon;
        UserGradeView layUserGrade;

        public GiftUserItemViewHolder(Context context, View itemView) {
            super(context, itemView);
            itemLayout = (RelativeLayout) itemView.findViewById(R.id.item_layout);
            rankPos = (TextView) itemView.findViewById(R.id.rank_pos_textview);
            userIcon = (ImageView) itemView.findViewById(R.id.user_icon);
            layUserGrade = itemView.findViewById(R.id.layUserGrade);
            userGiftRecord = (TextView) itemView.findViewById(R.id.user_gift_record);
            userGiftCoins = (TextView) itemView.findViewById(R.id.send_total_price);
            rankImg = (ImageView) itemView.findViewById(R.id.cp_rank_front_iv);
            bestGiftIcon = (SimpleDraweeView) itemView.findViewById(R.id.best_gift_icon);
        }

        @Override
        public void setViewHolderChildViewContent(MineAbstractBean bean, int position) {
            this.bean = (UserGiftRecordBean) bean;
            if (this.bean == null){
                return;
            }
            itemLayout.setOnClickListener(this);
            rankPos.setText((position+1)+"");
            if (this.bean.getFrom_user() != null){
                GlideUtil.displayCircleImageBorder(userIcon, this.bean.getFrom_user().getIcon());
                layUserGrade.setData(this.bean.getFrom_user(),false);
                int a = this.bean.getCount().length();
                Spannable spannable = new SpannableString(this.bean.getCount());
                spannable.setSpan(new ForegroundColorSpan(Color.parseColor("#ff6e70")), 0, this.bean.getCount().length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                SpannableStringBuilder sb = new SpannableStringBuilder();
                sb.append("送了");
                sb.append(spannable);
                sb.append("件礼物");
                userGiftRecord.setText(sb);
            }
            if (position < 3 && position >=0){
                rankImg.setVisibility(View.VISIBLE);
                switch (position){
                    case 0:
                        rankImg.setImageResource(R.drawable.cp_rank_first);
                        break;
                    case 1:
                        rankImg.setImageResource(R.drawable.cp_rank_second);
                        break;
                    case 2:
                        rankImg.setImageResource(R.drawable.cp_rank_third);
                        break;
                }
            }else {
                rankImg.setVisibility(View.GONE);
            }

            userGiftCoins.setText(this.bean.getCoins());

            if (this.bean.getGift() != null && this.bean.getGift().getAnimation_image() != null){
                ImageManager.loadImage(bestGiftIcon , this.bean.getGift().getAnimation_image() , 1.0f);
            }

        }


        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.item_layout:
                    if (bean != null){
                        Map keyMap = new HashMap();
                        keyMap.put("mid" , bean.getId());
                        keyMap.put("mtype" , bean.getObject_type() );
                        keyMap.put("mpos" , mPosition);
                        keyMap.put("muid" , bean.getFrom_user_id());
                        TMAnalysis.event(mContext , "enter_person_from_gift");
                        PersonalInfoService.enterOthersPersonalCenter(mContext , bean.getFrom_user_id());
                    }

                    break;
            }
        }


    }
}
